package com.epam.manager.backingbeans;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import com.epam.DAO.BillDAO;
import com.epam.entity.Bill;
import com.epam.entity.Request;
import com.epam.entity.Status;

public class ClosedRequestManagedBean extends RequestController {

	private Request request;
	private Bill bill;
	
	private DataModel<Bill> bills;
	
	@EJB
	private BillDAO billDAO;
		
	public ClosedRequestManagedBean() {
		super();
		request = new Request();
		bills = new ListDataModel<Bill>();
	}
	    
    @Override
	public DataModel<Request> getRequests() {
		requests.setWrappedData(requestDAO.getRequests(firstRow, rowsPerPage, Status.CLOSED));
		totalRows = requestDAO.getRequestsCount(Status.CLOSED);
		// Set currentPage, totalPages and pages.
		currentPage = (totalRows / rowsPerPage) - ((totalRows - firstRow) / rowsPerPage) + 1;
        totalPages = (totalRows / rowsPerPage) + ((totalRows % rowsPerPage != 0) ? 1 : 0);

		return requests;
	}
        	
	public DataModel<Bill> getBills() {
		List<Bill> list = billDAO.getRequestBills(requests.getRowData());
		bills.setWrappedData(list);
		return bills;
	}

	public void setBills(DataModel<Bill> bills) {
		this.bills = bills;
	}

	public Request getRequest() {
		return request;
	}

	public void setRequest(Request request) {
		this.request = request;
	}
	
	public Bill getBill() {
		return bill;
	}

	public void setBill(Bill bill) {
		this.bill = bill;
	}
		
	public void initRequest() {
		request = requests.getRowData();
	}
	
	public void initBill() {
		bill = bills.getRowData();
	}
	
	public void updateRequest() {
		requestDAO.update(request);
	}

}
